﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace TV_Agenda_V2
{
    public partial class ProgressDialog : Form
    {
        private delegate void ProgressDelegate(Int32 val);
        private delegate void CloseDelegate();
        private Thread thread;

        public ProgressDialog()
        {
            InitializeComponent();
        }

        public void CloseDialog()
        {
            if (this.InvokeRequired)
                this.Invoke(new CloseDelegate(Close));
            else
                this.Close();
        }
        
        /// <summary>
        /// Starts the timer.
        /// </summary>
        /// <param name="seconds">The amount of seconds to wait.</param>
        public void StartTimer(Int32 seconds)
        {
            SetMaximum(seconds);
            thread = new Thread(new ParameterizedThreadStart(Start));
            thread.Start(seconds);
        }

        /// <summary>
        /// Starts updating the progress bar every second.
        /// </summary>
        /// <param name="seconds">The amount of seconds to wait for.</param>
        private void Start(object seconds)
        {
            Int32 secs = (Int32)seconds;
            DateTime start = DateTime.Now;
            TimeSpan span = start - DateTime.Now;
            while (span.Seconds < secs)
            {
                SetProgress(span.Seconds);
                Thread.Sleep(300);
                span = start - DateTime.Now;
            }
            this.Close();
        }

        /// <summary>
        /// Sets the progress.
        /// </summary>
        /// <param name="progress">The progress.</param>
        public void SetProgress(Int32 progress)
        {
            if (progressBar.InvokeRequired)
            {
                progressBar.BeginInvoke(new ProgressDelegate(SetProg), progress);
            }
            else
            {
                SetProg(progress);
            }
        }

        /// <summary>
        /// Sets the progress (invoke).
        /// </summary>
        /// <param name="progress">The progress.</param>
        private void SetProg(Int32 progress)
        {
            if (progress < progressBar.Minimum)
                progressBar.Value = progressBar.Minimum;
            else if (progress > progressBar.Maximum)
                progressBar.Value = progressBar.Maximum;
            else
                progressBar.Value = progress;
        }

        /// <summary>
        /// Sets the maximum.
        /// </summary>
        /// <param name="max">The max.</param>
        public void SetMaximum(Int32 max)
        {
            if (progressBar.InvokeRequired)
            {
                progressBar.BeginInvoke(new ProgressDelegate(SetMax), max);
            }
            else
            {
                SetMax(max);
            }
        }

        /// <summary>
        /// Sets the maximum (invoke).
        /// </summary>
        /// <param name="max">The max.</param>
        private void SetMax(Int32 max)
        {
            progressBar.Maximum = max;
        }

        /// <summary>
        /// Handles the FormClosing event of the ProgressDialog control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void ProgressDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            StopThread();
        }

        /// <summary>
        /// Stops the thread.
        /// </summary>
        private void StopThread()
        {
            if (thread == null)
                return;

            try
            {
                thread.Abort();
            }
            catch (Exception) { };
        }
    }
}
